import https from "../https/Https.js";
import cmd from "../Cmd.js";

var printCtrl = {
	//模板id 241mm A4纸
	MOD_241: 241,
	//模板id 80mm
	MOD_80: 80,
	//模板 58mm
	MOD_58: 50,
	//销售模板
	XS_MOD: "xs",
	//采购模板
	CG_MOD: "cg",
	//ck: "出库单"
	CK_MOD: "ck",
	//rk: "入库单",
	RK_MOD: "rk",
	//db: "调拨单"
	DB_MOD: "db",

	cpid: "",
	printSize: 0,
};

/*****获取打印模板尺寸****/
printCtrl.getPrintSize = async function (cpid, callBack) {
	let that = this;
	this.cpid = cpid;
	let param = {};
	param.cpid = cpid;
	https.getLocalPrintConfig(param, back);
	function back(data) {
		if (!data) return;
		if (data.status == 1) {
			if (!data.data) return;
			data = data.data;
			if (data.tpl) {
				let ps = data.tpl.paper_wh;
				let arr = String(ps).split("|");
				let printSize = arr[0];
				if (printSize == 210)
					printSize = that.MOD_241;
				that.printSize = printSize;
				if (callBack)
					callBack.apply(null, [printSize]);
			}
		}
	};
}

/***调用云打印***/
/******
type:  表单类型（模块）：如xs=销售单；cg=采购；ck=出库单；rk=入库单；db=调拨单; jh=减货单;yy=预约单
type12 :单据类型；type=xs或cg/时传值；1=销售/采购；2=退货， type=ck或rk或db或jh时传值, 1. 入库单/出库单/调拨单/减货单，预约单不用传该参数
odid : 单据id
storeid : 	仓库id，没有仓库打印时传仓库id
****/
printCtrl.doYunPrint = async function (data, callBack) {
	https.getLocalPrintConfig(param, back);
	function callBack(data) {
		if (!data) return;
		if (callBack)
			callBack.apply(null, [printSize]);
	}
}

/******调用模板*******
cpid:企业id,
orderId:订单id,
type:表单类型（模块）：如xs=销售单；cg=采购；ck=出库单；rk=入库单；db=调拨单; jh=减货单;yy=预约单,
type_od:单据类型；type=xs或cg/时传值；1=销售/采购；2=退货， type=ck或rk或db或jh时传值, 1. 入库单/出库单/调拨单/减货单，预约单不用传该参数
*******/

printCtrl.print = function (data) {

	var iTop = (window.screen.height - 842) / 2; //获得窗口的垂直位置;
	var iLeft = (window.screen.width - 915) / 2; //获得窗口的水平位置;

	let url = data.url + data.cpid + "&odid=" + data.orderId + "&type=" + data.type + "&type_od=" + data.type_od + "&pview=" + (data.pview || 0) + "&v=" + new Date().getTime();
	console.log("订单打印url====:", url);
	// cmd.external(cmd.CMD_PRINT,url);//旧的PC 打印接口，已经废弃
	window.open(url, "LoginWindow", "location=0,resizable=0,status=0,menubar=0,scrollbars=1" + "width=" + 915 + ",height=" + 842 + "," + "left=" + iLeft + ",top=" + iTop);
}
export default printCtrl;